package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * @Author wpj
 * @Date 2020/8/19
 */
public class GetCurDayChargeExcel extends BaseRepair {

    public GetCurDayChargeExcel(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        List<Map<String, String>> listBoy = new ArrayList<>();
        List<Map<String, String>> listGirl = new ArrayList<>();
        long startTime = 1595952000;
        long oneDaySeconds = 3600 * 24;
        for (int i = 0; i < 18; i++) {
            int numBoyRegister = getTodayRegisterNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 1);
            int numGirlRegister = getTodayRegisterNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 2);
            Set<Long> boySet = new HashSet<>();
            Set<Long> girlSet = new HashSet<>();
            List<Long> boyList = getTodayChargeNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 1);
            List<Long> girlList = getTodayChargeNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 2);
            List<Long> boyThirdList = getTodayThirdChargeNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 1);
            List<Long> girlThirdList = getTodayThirdChargeNum(startTime + oneDaySeconds * i, startTime + oneDaySeconds * (i + 1), (short) 2);
            boySet.addAll(boyList);
            boySet.addAll(boyThirdList);
            girlSet.addAll(girlList);
            girlSet.addAll(girlThirdList);
            int numBoyCharge = boySet.size();
            int numGirlCharge = girlSet.size();
            getLogger().error("{} day - {} : {} / {}", getGameServer().getServerInfo().getServerId(), i + 1, numBoyCharge, numBoyRegister);
            getLogger().error("{} day - {} : {} / {}", getGameServer().getServerInfo().getServerId(), i + 1, numGirlCharge, numGirlRegister);
            Map<String, String> mapBoy = new HashMap<>();
            Map<String, String> mapGirl = new HashMap<>();
            mapBoy.put("serverId", getGameServer().getServerInfo().getServerId() + "");
            mapGirl.put("serverId", getGameServer().getServerInfo().getServerId() + "");
            mapBoy.put("sex", 1 + "");
            mapGirl.put("sex", 2 + "");
            mapBoy.put("day", i + 1 + "");
            mapGirl.put("day", i + 1 + "");
            mapBoy.put("chargeNum", numBoyCharge + "");
            mapGirl.put("chargeNum", numGirlCharge + "");
            mapBoy.put("registerNum", numBoyRegister + "");
            mapGirl.put("registerNum", numGirlRegister + "");

            listBoy.add(mapBoy);
            listGirl.add(mapGirl);
        }
        DataMgr.addData(listBoy);
        DataMgr.addData(listGirl);
    }


    public List<Long> getTodayThirdChargeNum(long startTime, long endTime, short sex) {
        List<Long> list = new ArrayList<>();
        String startDateString = DateHelper.getCurrentDateTimeString(startTime * 1000);
        String endDateString = DateHelper.getCurrentDateTimeString(endTime * 1000);
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select s3.userId as userId from \n" +
                    "(select s1.userId as userId ,s1.sex as sex, s1.registerTime as rTime,s2.orderStatus as os,s2.updateTime as updateTime from (select userId,sex,registerTime from t_u_user where sex > 0 and registerTime >= ? and registerTime < ?)s1 LEFT JOIN \n" +
                    "(select userId, orderStatus, updateTime from t_u_thirdparty_recharge where orderStatus > 0 and orderStatus <= 2 and updateTime >= ? and updateTime < ?)s2 on s1.userId = s2.userId)s3\n" +
                    "where os > 0 and sex = ? GROUP BY s3.userId;");
            call.setLong(1, startTime);
            call.setLong(2, endTime);
            call.setString(3, startDateString);
            call.setString(4, endDateString);
            call.setShort(5, sex);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("userId"));
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return list;
        }
    }


    public List<Long> getTodayChargeNum(long startTime, long endTime, short sex) {
        List<Long> list = new ArrayList<>();
        String startDateString = DateHelper.getCurrentDateTimeString(startTime * 1000);
        String endDateString = DateHelper.getCurrentDateTimeString(endTime * 1000);
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select s3.userId as userId from \n" +
                    "(select s1.userId as userId ,s1.sex as sex, s1.registerTime as rTime,s2.orderStatus as os,s2.updateTime as updateTime from (select userId,sex,registerTime from t_u_user where sex > 0 and registerTime >= ? and registerTime < ?)s1 LEFT JOIN \n" +
                    "(select userId, orderStatus, updateTime from t_u_recharge_detail where orderStatus > 0 and orderStatus <= 2 and updateTime >= ? and updateTime < ?)s2 on s1.userId = s2.userId)s3\n" +
                    "where os > 0 and sex = ? GROUP BY s3.userId;");
            call.setLong(1, startTime);
            call.setLong(2, endTime);
            call.setString(3, startDateString);
            call.setString(4, endDateString);
            call.setShort(5, sex);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("userId"));
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return list;
        }
    }

    public int getTodayRegisterNum(long startTime, long endTime, short sex) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select count(*) as num from t_u_user where registerTime >= ? and registerTime < ? and sex = ?");
            call.setLong(1, startTime);
            call.setLong(2, endTime);
            call.setShort(3, sex);
            rs = call.executeQuery();
            while (rs.next()) {
                return rs.getInt("num");
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return 0;
    }


    private List<UserInfo> getUserInfoList() {
        List<UserInfo> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select userId,sex,registerTime from t_u_user where registerTime >= 1595952000 and registerTime < 1597593600");
            rs = call.executeQuery();
            while (rs.next()) {
                UserInfo userInfo = new UserInfo();
                long userId = rs.getLong("userId");
                short sex = rs.getShort("sex");
                long time = rs.getLong("registerTime");
                userInfo.setUserId(userId);
                userInfo.setSex(sex);
                userInfo.setRegisterTime(time);
                dataList.add(userInfo);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }
}
